#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include<unistd.h>
#include "Webskt.hpp"

using namespace std;
// 存储所有连接的 WebSocket 上下文
vector<struct lws *> client_lws;

struct lws_context_creation_info info;
int my_callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);


static struct lws_protocols protocols[] = {
    {
        "my-protocol", // 协议名称
        my_callback,   // 事件回调函数
        0,          // 每个会话的数据大小
        0,          // 接收缓冲区大小
    },
    {NULL, NULL, 0, 0} // 结束标记
};

void SendMsg(string &msg)
{
    for (auto &e : client_lws)
    {
        // lws_write(e, (unsigned char *)in, len, LWS_WRITE_TEXT);
        lws_write(e, (unsigned char *)msg.c_str(), msg.length(), LWS_WRITE_TEXT);
    }
}
vector<string> str;
int strsize = 0;
int maxsize = 10000;
Respond respond;

vector<string>* sqldata;
int my_callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
{
    switch (reason)
    {
    case LWS_CALLBACK_ESTABLISHED:
    {
        printf("连接建立\n");
        sqldata=new vector<string>();
        Mysqlconnect con;
        con.Connect();
        con.LoadData(*sqldata);
        for (auto &e : *sqldata)
        {
            lws_write(wsi, (unsigned char *)e.c_str(), e.length(), LWS_WRITE_TEXT);
            usleep(10000);
        }
        
        clientonline.Addclient(wsi);
        client_lws.push_back(wsi);
        break;
    }
    case LWS_CALLBACK_RECEIVE:
    {
        // 回显数据
        try
        {
            if(in==nullptr) break;
            str.push_back(string((char*)in));
            str[str.size()-1] = str[str.size()-1].substr(0, len);
            cout << "接收到数据：" <<str[strsize] << endl;
            // cout << "接收到数据：" << string((char*)in) << endl;
            // string*str1=new string((char*)in);
            // lws_write(wsi, (unsigned char *)in, len, LWS_WRITE_TEXT);
            // lws_write(wsi, (unsigned char *)str.c_str(), str.length(), LWS_WRITE_TEXT);
            
            // for (auto &e : client_lws)
            // {
            //     // lws_write(e, (unsigned char *)in, len, LWS_WRITE_TEXT);
            //     lws_write(e, (unsigned char *)str[str.size()-1].c_str(),str[str.size()-1].length() ,LWS_WRITE_TEXT);
            // }

            clientonline.Bordcast(str[str.size()-1] );

            // SendMsg(str);

            // strsize = strsize % maxsize;
            // cout << "发送数据成功" << endl;
            // delete str1;

            // respond.Deserialize(str[str.size()-1]);
            // respond.Serialize(str[str.size()-1]);
            // respond.Print();
            // respond.Handle();
            // cout<<"发送数据成功"<<endl;
            // strsize++;
            break;
        }
        catch (exception &e)
        {
            cout << "发送数据有问题" << e.what() << endl;
        }
    }
    case LWS_CALLBACK_CLOSED:
    {

        printf("连接关闭\n");
        clientonline.Deleteclient(wsi);
        break;
    }
    default:
        break;
    }
    return 0;
}

void initserver()
{
    memset(&info, 0, sizeof(info));
    info.port = 8080;
    info.protocols = protocols;
    info.gid = -1;
    info.uid = -1;
}

int main()
{
    initserver();
    struct lws_context *context = lws_create_context(&info);
    if (!context)
    {
        cout << "创建服务器失败" << endl;
        return -1;
    }
    cout << "启动服务器成功" << endl;

    while (1)
    {
        lws_service(context, 50);
    }
    lws_context_destroy(context);
    return 0;
}
